#include <gflags/gflags.h>
#include <gtest/gtest.h>

#include <thread>

#include "channel.h"
#include "etcd.h"
#include "friend.pb.h"
#include "log.h"
#include "uuid.h"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(friend_service, "/service/friend", "服务监控根目录");

chat::ServiceManagerPtr sm;

void apply_test(const std::string &uid1, const std::string &uid2) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::FriendAddReq req;
    chat::FriendAddRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    req.set_respondent_id(uid2);
    brpc::Controller cntl;
    stub.FriendAdd(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}

void get_apply_list(const std::string &uid1) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::GetPendingFriendEventListReq req;
    chat::GetPendingFriendEventListRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    brpc::Controller cntl;
    stub.GetPendingFriendEventList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.event_size(); i++) {
        std::cout << "---------------\n";
        std::cout << rsp.event(i).sender().user_id() << std::endl;
        std::cout << rsp.event(i).sender().nickname() << std::endl;
        std::cout << rsp.event(i).sender().avatar() << std::endl;
    }
}

void process_apply_test(const std::string &uid1, bool agree,
                        const std::string &apply_user_id) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::FriendAddProcessReq req;
    chat::FriendAddProcessRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    req.set_agree(agree);
    req.set_apply_user_id(apply_user_id);
    brpc::Controller cntl;
    stub.FriendAddProcess(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    if (agree) {
        std::cout << rsp.new_session_id() << std::endl;
    }
}

void search_test(const std::string &uid1, const std::string &key) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::FriendSearchReq req;
    chat::FriendSearchRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    req.set_search_key(key);
    brpc::Controller cntl;
    stub.FriendSearch(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.user_info_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.user_info(i).user_id() << std::endl;
        std::cout << rsp.user_info(i).nickname() << std::endl;
        std::cout << rsp.user_info(i).avatar() << std::endl;
    }
}

void friend_list_test(const std::string &uid1) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::GetFriendListReq req;
    chat::GetFriendListRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    brpc::Controller cntl;
    stub.GetFriendList(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.friend_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.friend_list(i).user_id() << std::endl;
        std::cout << rsp.friend_list(i).nickname() << std::endl;
        std::cout << rsp.friend_list(i).avatar() << std::endl;
    }
}

void remove_test(const std::string &uid1, const std::string &uid2) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::FriendRemoveReq req;
    chat::FriendRemoveRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    req.set_peer_id(uid2);
    brpc::Controller cntl;
    stub.FriendRemove(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
}
void create_css_test(const std::string &uid1,
                     const std::vector<std::string> &uidlist) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::ChatSessionCreateReq req;
    chat::ChatSessionCreateRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    req.set_chat_session_name("快乐一家人");
    for (auto &id : uidlist) {
        req.add_member_id_list(id);
    }
    brpc::Controller cntl;
    stub.ChatSessionCreate(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    std::cout << rsp.chat_session_info().chat_session_id() << std::endl;
    std::cout << rsp.chat_session_info().chat_session_name() << std::endl;
}

void cssmember_test(const std::string &uid1, const std::string &cssid) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::GetChatSessionMemberReq req;
    chat::GetChatSessionMemberRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    req.set_chat_session_id(cssid);
    brpc::Controller cntl;
    stub.GetChatSessionMember(&cntl, &req, &rsp, nullptr);
    ASSERT_FALSE(cntl.Failed());
    ASSERT_TRUE(rsp.success());
    for (int i = 0; i < rsp.member_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.member_info_list(i).user_id() << std::endl;
        std::cout << rsp.member_info_list(i).nickname() << std::endl;
        std::cout << rsp.member_info_list(i).avatar() << std::endl;
    }
}

void csslist_test(const std::string &uid1) {
    auto channel = sm->choose(FLAGS_friend_service);
    if (!channel) {
        std::cout << "获取通信信道失败！" << std::endl;
        return;
    }
    chat::FriendService_Stub stub(channel.get());
    chat::GetChatSessionListReq req;
    chat::GetChatSessionListRsp rsp;
    req.set_request_id(chat::Uuid::uuid());
    req.set_user_id(uid1);
    brpc::Controller cntl;
    std::cout << "发送获取聊天会话列表请求！！\n";
    stub.GetChatSessionList(&cntl, &req, &rsp, nullptr);
    std::cout << "请求发送完毕1！！\n";
    ASSERT_FALSE(cntl.Failed());
    std::cout << "请求发送完毕2！！\n";
    ASSERT_TRUE(rsp.success());
    std::cout << "请求发送完毕，且成功！！\n";
    for (int i = 0; i < rsp.chat_session_info_list_size(); i++) {
        std::cout << "-------------------\n";
        std::cout << rsp.chat_session_info_list(i).single_chat_friend_id()
                  << std::endl;
        std::cout << rsp.chat_session_info_list(i).chat_session_id()
                  << std::endl;
        std::cout << rsp.chat_session_info_list(i).chat_session_name()
                  << std::endl;
        std::cout << rsp.chat_session_info_list(i).avatar() << std::endl;
        std::cout << "消息内容：\n";
        std::cout << rsp.chat_session_info_list(i).prev_message().message_id()
                  << std::endl;
        std::cout
            << rsp.chat_session_info_list(i).prev_message().chat_session_id()
            << std::endl;
        std::cout << rsp.chat_session_info_list(i).prev_message().timestamp()
                  << std::endl;
        std::cout
            << rsp.chat_session_info_list(i).prev_message().sender().user_id()
            << std::endl;
        std::cout
            << rsp.chat_session_info_list(i).prev_message().sender().nickname()
            << std::endl;
        std::cout
            << rsp.chat_session_info_list(i).prev_message().sender().avatar()
            << std::endl;
        std::cout << rsp.chat_session_info_list(i)
                         .prev_message()
                         .message()
                         .file_message()
                         .file_name()
                  << std::endl;
        std::cout << rsp.chat_session_info_list(i)
                         .prev_message()
                         .message()
                         .file_message()
                         .file_contents()
                  << std::endl;
    }
}

int main(int argc, char *argv[]) {
    google::ParseCommandLineFlags(&argc, &argv, true);
    chat::initLogger(FLAGS_log_level, FLAGS_run_mode, FLAGS_log_file);

    // 1. 先构造Rpc信道管理对象
    sm = std::make_shared<chat::ServiceManager>();
    sm->declared(FLAGS_friend_service);
    auto put_cb = std::bind(&chat::ServiceManager::onServiceOnline, sm.get(),
                            std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&chat::ServiceManager::onServiceOffline, sm.get(),
                            std::placeholders::_1, std::placeholders::_2);
    // 2. 构造服务发现对象
    chat::DiscoveryPtr dclient = std::make_shared<chat::Discovery>(
        FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);

    // // 佩奇 -> 乔治
    // apply_test("3c112845-20ee-fc5c-0000-000000000001",
    //            "d2e2a77e-8270-1e43-0000-000000000003");

    // // 猪爸爸 -> 乔治
    // apply_test("cdaf2efd-7f3b-a31f-0000-000000000001",
    //            "d2e2a77e-8270-1e43-0000-000000000003");

    // // 乔治 -> 佩奇
    // apply_test("d2e2a77e-8270-1e43-0000-000000000003",
    //            "3c112845-20ee-fc5c-0000-000000000001");

    // get_apply_list("d2e2a77e-8270-1e43-0000-000000000003");

    // // 乔治 同意 佩奇
    // process_apply_test("d2e2a77e-8270-1e43-0000-000000000003", true,
    //                    "3c112845-20ee-fc5c-0000-000000000001");

    // // 乔治 拒绝 乔治
    // process_apply_test("d2e2a77e-8270-1e43-0000-000000000003", false,
    //                    "cdaf2efd-7f3b-a31f-0000-000000000001");

    // // 佩奇 同意 乔治
    // process_apply_test("3c112845-20ee-fc5c-0000-000000000001", true,
    //                    "d2e2a77e-8270-1e43-0000-000000000003");

    // std::cout << "**********************\n";
    // search_test("d2e2a77e-8270-1e43-0000-000000000003", "猪");  // 小猪佩奇
    // std::cout << "++++++++++++++++++++++\n";
    // search_test("b8b449aa-2b29-8f28-0000-000000000003", "猪");  // 猪妈妈
    // std::cout << "======================\n";
    // search_test("d2e2a77e-8270-1e43-0000-000000000003", "乔治");  // 小猪乔治

    // friend_list_test("3c112845-20ee-fc5c-0000-000000000001");   // 小猪佩奇
    // std::cout << "++++++++++++++++++++++\n";
    // friend_list_test("d2e2a77e-8270-1e43-0000-000000000003");   // 小猪乔治
    // std::cout << "++++++++++++++++++++++\n";
    // friend_list_test("cdaf2efd-7f3b-a31f-0000-000000000001");   // 猪爸爸

    // remove_test("3c112845-20ee-fc5c-0000-000000000001",
    // "d2e2a77e-8270-1e43-0000-000000000003");

    // std::vector<std::string> uidlist = {
    //     "3c112845-20ee-fc5c-0000-000000000001",
    //     "d2e2a77e-8270-1e43-0000-000000000003",
    //     "cdaf2efd-7f3b-a31f-0000-000000000001",
    //     "b8b449aa-2b29-8f28-0000-000000000003"};
    // create_css_test("b8b449aa-2b29-8f28-0000-000000000003", uidlist);

    // cssmember_test("d2e2a77e-8270-1e43-0000-000000000003",
    // "5acdbc15-fc9c-ff84-0000-000000000007"); std::cout <<
    // "++++++++++++++++++++++\n";
    // cssmember_test("b8b449aa-2b29-8f28-0000-000000000003",
    // "5acdbc15-fc9c-ff84-0000-000000000007");

    csslist_test("cdaf2efd-7f3b-a31f-0000-000000000001");
    return 0;
}